import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../common/index.dart';
import '../config/index.dart';
import '../tools/index.dart';

/// 6.1
Size screenSize = const Size(390, 844);

/// 初始化MaterialApp
Widget initMaterialApp({
  Widget Function(BuildContext, Widget?)? builder,
}) {
  return GetMaterialApp(
    /// 入口路由
    initialRoute: RouterS.configNoramlRouts(),

    /// 所有路由集合
    getPages: RouterS.getAllRoutS(),

    /// 默认路由动画
    defaultTransition: Transition.rightToLeft,

    /// 主题
    theme: themeDataLight,

    /// 暗黑主题
    darkTheme: themeDataDark,

    /// 是否使用继承的 MediaQuery
    /// 适配刘海屏
    useInheritedMediaQuery: true,

    /// 路由监听
    navigatorObservers: [
      AppRouterObserver().routeObserver,
    ],
    themeMode: ThemeTool().getLocalProfileThemeModel(),

    /// 是否显示 导航栏右上角 debug 标识
    debugShowCheckedModeBanner: false,
    builder: builder,
  );
}

/// 初始屏幕适配
Widget initScreenUtil({
  required Widget Function(BuildContext, Widget?) builder,
  bool useInheritedMediaQuery = true,
}) {
  return ScreenUtilInit(
    /// UI尺寸
    designSize: screenSize,
    useInheritedMediaQuery: useInheritedMediaQuery,
    minTextAdapt: true,
    splitScreenMode: true,
    builder: builder,
  );
}

Widget initGestureDetector(BuildContext context, Widget child) {
  return GestureDetector(
    onTap: () {
      FocusScope.of(context).requestFocus(FocusNode());
    },
    child: child,
  );
}

/// 初始化App
Widget initRunApp() {
  final easyLoading = EasyLoading.init();
  Widget body = initMaterialApp(builder: (context, child) {
    return initScreenUtil(
      builder: (p0, p1) {
        Widget body = initGestureDetector(context, child ?? Container());
        return easyLoading(context, body);
      },
    );
  });
  return body;
}
